package com.vtb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.base.VtbException;
import com.vtb.entity.RrDetailInfo;
import com.vtb.entity.RrOauth;
import com.vtb.inter.VtbDaoInterface;
import com.vtb.utils.JdbcUtils;

/**
 * RrDao 实现类
 *
 * @author Chen Tao
 * @date 2012-7-1 下午05:00:49
 */
public class RrDaoImpl implements VtbDaoInterface<RrOauth, RrDetailInfo> {
	private static final Log log = LogFactory.getLog(RrDaoImpl.class);

    @Override
    public void del(RrOauth rrOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "delete from rrdata where uni=?";
            qr.update(JdbcUtils.getConnection(), sql, rrOauth.getUni());
        } catch (SQLException e) {
            log.error("删除rrdata错误：", e);
            throw new VtbException("删除rrdata错误"+e);
        }
        
    }

    @SuppressWarnings("deprecation")
    @Override
    public RrOauth get(RrOauth rrOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from rrdata where uni=?";
            rrOauth = (RrOauth) qr.query(JdbcUtils.getConnection(), sql,rrOauth.getUni(),new BeanHandler(RrOauth.class));
            return rrOauth;
        } catch (SQLException e) {
            log.error("通过rroauth查找rrdata错误：", e);
            throw new VtbException("通过rroauth查找rrdata错误"+e);
        }
    }

    @SuppressWarnings("unchecked")
    public List<RrOauth> getAll() {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from rrdata";
            return (List<RrOauth>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(RrOauth.class));
        } catch (SQLException e) {
            log.error("查询rrdata所有信息：", e);
            throw new VtbException("查询rrdata所有信息"+e);
        }
    }
    @SuppressWarnings("unchecked")
    public List<RrDetailInfo> getAllInfo() {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from rrdata order by createTime DESC";
            return (List<RrDetailInfo>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(RrDetailInfo.class));
        } catch (SQLException e) {
            log.error("查询rrdata所有信息：", e);
            throw new VtbException("查询rrdata所有信息"+e);
        }
    }

    @Override
    public void insert(RrOauth t) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void insertAll(RrDetailInfo rrDetailInfo) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "insert into rrdata(uni,access_token,refresh_token,expires_in,createTime,refq,refs,r2q,r2s,name,sex,hometown,university) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {rrDetailInfo.getUni(),rrDetailInfo.getAccess_token(),rrDetailInfo.getRefresh_token(),rrDetailInfo.getExpires_in(),rrDetailInfo.getCreateTime(),rrDetailInfo.getRefq(),rrDetailInfo.getRefs(),rrDetailInfo.getR2q(),rrDetailInfo.getR2s(),rrDetailInfo.getName(),rrDetailInfo.getSex(),rrDetailInfo.getHometown(),rrDetailInfo.getUniversity()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("插入rrdata错误：", e);
            throw new VtbException("插入rrdata错误"+e);
        }

        
    }

    @Override
    public void update(RrOauth rrOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "update rrdata set access_token=?,refresh_token=?,expires_in=?,createTime=?,refq=?,refs=?,r2s=?,r2q=? where uni=?";
            Object[] params = {rrOauth.getAccess_token(),rrOauth.getRefresh_token(),rrOauth.getExpires_in(),rrOauth.getCreateTime(),rrOauth.getRefq(),rrOauth.getRefs(),rrOauth.getR2s(),rrOauth.getR2q(),rrOauth.getUni()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("更新rrdata错误：", e);
            throw new VtbException("更新rrdata错误"+e);
        }
        
    }
    
    public void update(RrDetailInfo rrDetailInfo) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "update rrdata set name=?,sex=?,hometown=?,university=? where uni=?";
            Object[] params = {rrDetailInfo.getName(),rrDetailInfo.getSex(),rrDetailInfo.getHometown(),rrDetailInfo.getUniversity(),rrDetailInfo.getUni()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("更新rrdata错误：", e);
            throw new VtbException("更新rrdata错误"+e);
        }
    }
	
}
